home *** CD-ROM | disk | FTP | other *** search
/ Developer CD Series 1997 May: Tool Chest / Dev.CD May 97 TC.toast / Sample Code / Snippets / Networking / AFP C++ / FindServer.cp < prev    next >
Encoding:
Text File  |  1992-05-01  |  2.8 KB  |  110 lines  |  [TEXT/MPS ]

  1. /************************************************************
  2.  
  3. Created: Tuesday, November 6, 1991
  4.     FindServer.cp
  5.     C++ Interface to the AppleTalk Filing Protocol
  6.     M.Vierling
  7.  
  8.  
  9.         Copyright Apple Computer, Inc. 1991-1992
  10.         All rights reserved
  11.  
  12. ************************************************************/
  13.  
  14. #include <stream.h>
  15. #include <Errors.h>
  16. #include <strings.h>
  17. #include <AppleTalk.h>
  18.  
  19. AddrBlock FindServer( char * zoneName, char * serverName )
  20. {
  21.     MPPPBPtr    thePBptr;
  22.     Ptr            theRetBuffPtr;
  23.     char *      serverType = "AFPServer";
  24.     EntityPtr    entityPtr;
  25.     short        numGotten;
  26.     Str255        myServerType;
  27.     Str255        myZoneName;
  28.     Str255        myServerName;
  29.     OSErr        ErrResult;
  30.     AddrBlock     theAddress;
  31.     char        AspTimeout = 10;
  32.     char        AspRetry = 10;
  33.     
  34.     theAddress.aNet = 0;
  35.     theAddress.aNode = 0;
  36.     theAddress.aSocket = 0;
  37.  
  38.     if ((entityPtr = new EntityName) == nil) {
  39.         return theAddress;
  40.     }
  41.  
  42.     strcpy( (char *)myZoneName, zoneName );
  43.     strcpy( (char *)myServerType, serverType );
  44.     strcpy( (char *)myServerName, serverName );
  45.     c2pstr( (char *)myServerType );
  46.     c2pstr( (char *)myServerName );
  47.     c2pstr( (char *)myZoneName );
  48.     
  49.     NBPSetEntity( (Ptr)entityPtr, (Ptr)myServerName, (Ptr)myServerType, (Ptr)myZoneName );
  50.     
  51.     if ((thePBptr = new MPPParamBlock) == nil) {
  52.         return theAddress;
  53.     }
  54.     if ((theRetBuffPtr = (Ptr)(new char[4096])) == nil) {
  55.         return theAddress;
  56.     }
  57.     thePBptr->NBP.ioRefNum =                     0;
  58.     thePBptr->NBP.interval =                     AspTimeout;
  59.     thePBptr->NBP.count    =                         AspRetry;
  60.     thePBptr->NBP.NBPPtrs.entityPtr    =             (Ptr)entityPtr;
  61.     thePBptr->NBP.parm.Lookup.retBuffPtr =         theRetBuffPtr;
  62.     thePBptr->NBP.parm.Lookup.retBuffSize =     4096;
  63.     thePBptr->NBP.parm.Lookup.maxToGet    =         1;
  64.  
  65.     ErrResult = PLookupName( thePBptr, false );
  66.     if (ErrResult)
  67.     {
  68.         cerr << "FindServer: PLookupName Error = " << ErrResult << endl;
  69.         return theAddress;
  70.     }
  71.     ErrResult = thePBptr->NBP.ioResult;
  72.     if (ErrResult)
  73.     {
  74.         cerr << "FindServer: ioResult Error = " << ErrResult << endl;
  75.         return theAddress;
  76.     }
  77.     numGotten = thePBptr->NBP.parm.Lookup.numGotten;
  78.     if (numGotten != 1)
  79.     {
  80.         ErrResult = afpNoServer;
  81.         cerr << "FindServer: Error PLookupName numGotten = " << numGotten << endl;
  82.         return theAddress;
  83.     }
  84.     ErrResult = NBPExtract( theRetBuffPtr, 1, 1, entityPtr, &theAddress );
  85.     if (ErrResult)
  86.     {
  87.         cerr << "FindServer: NBPExtract Error = " << ErrResult << endl;
  88.         return theAddress;
  89.     }
  90.     ErrResult = thePBptr->NBP.ioResult;
  91.     if (ErrResult)
  92.     {
  93.         cerr << "FindServer: ioResult Error = " << ErrResult << endl;
  94.         return theAddress;
  95.     }
  96.     
  97. //    cerr << "Server name:";
  98. //    cerr << p2cstr( (*entityPtr).objStr ) << endl;
  99. //    cerr << "Net: " << (long)theAddress.aNet << endl;
  100. //    cerr << "Node: " << (long)theAddress.aNode << endl;
  101. //    cerr << "Socket: " << (long)theAddress.aSocket << endl;
  102.  
  103.     delete entityPtr;
  104.     delete thePBptr;
  105.     delete [] theRetBuffPtr;
  106.     
  107.     return theAddress;
  108. }
  109.  
  110.